#ifndef FILEPICKER_H
#define FILEPICKER_H

#include <QObject>
#include <QString>
#include <QStringList>
#include <QUrl>
#include <QFileInfo>
#include <QDir>
#include <QSettings>

class FilePicker : public QObject
{
    Q_OBJECT
    
    Q_PROPERTY(QString selectedFilePath READ selectedFilePath NOTIFY fileSelected)
    Q_PROPERTY(QString selectedFileName READ selectedFileName NOTIFY fileSelected)
    Q_PROPERTY(QString fileSize READ fileSize NOTIFY fileSelected)
    Q_PROPERTY(QString fileExtension READ fileExtension NOTIFY fileSelected)
    Q_PROPERTY(bool hasSelection READ hasSelection NOTIFY selectionChanged)
    Q_PROPERTY(QStringList selectedFiles READ selectedFiles NOTIFY filesSelected)
    Q_PROPERTY(QString selectedDirectory READ selectedDirectory NOTIFY directorySelected)
    Q_PROPERTY(QStringList recentFiles READ recentFiles NOTIFY recentFilesChanged)
    Q_PROPERTY(QString fileType READ fileType NOTIFY fileSelected)
    Q_PROPERTY(QString fileModifiedTime READ fileModifiedTime NOTIFY fileSelected)
    
public:
    explicit FilePicker(QObject *parent = nullptr);
    ~FilePicker();
    
    // 属性访问器
    QString selectedFilePath() const { return m_filePath; }
    QString selectedFileName() const { return m_fileName; }
    QString fileSize() const { return m_fileSize; }
    QString fileExtension() const { return m_fileExtension; }
    bool hasSelection() const { return !m_filePath.isEmpty(); }
    QStringList selectedFiles() const { return m_selectedFiles; }
    QString selectedDirectory() const { return m_selectedDirectory; }
    QStringList recentFiles() const { return m_recentFiles; }
    QString fileType() const { return m_fileType; }
    QString fileModifiedTime() const { return m_fileModifiedTime; }
    
    // QML可调用方法
    Q_INVOKABLE void clearSelection();
    Q_INVOKABLE QString getFileInfo(const QString &filePath);
    Q_INVOKABLE bool fileExists(const QString &filePath);
    Q_INVOKABLE QString getDirectory(const QString &filePath);
    // 从QML FileDialog选择文件后调用此方法
    Q_INVOKABLE void setSelectedFile(const QString &filePath);
    // 多文件选择
    Q_INVOKABLE void setSelectedFiles(const QStringList &filePaths);
    // 文件夹选择
    Q_INVOKABLE void setSelectedDirectory(const QString &dirPath);
    // 文件操作
    Q_INVOKABLE bool copyFile(const QString &sourcePath, const QString &destPath);
    Q_INVOKABLE bool moveFile(const QString &sourcePath, const QString &destPath);
    Q_INVOKABLE bool deleteFile(const QString &filePath);
    Q_INVOKABLE bool createDirectory(const QString &dirPath);
    // 文件内容读取
    Q_INVOKABLE QString readTextFile(const QString &filePath, int maxSize = 1024 * 1024);
    Q_INVOKABLE bool writeTextFile(const QString &filePath, const QString &content);
    // 最近文件管理
    Q_INVOKABLE void addToRecentFiles(const QString &filePath);
    Q_INVOKABLE void clearRecentFiles();
    Q_INVOKABLE void removeRecentFile(const QString &filePath);
    // 文件类型检测
    Q_INVOKABLE QString getFileType(const QString &filePath);
    Q_INVOKABLE QString getFileIcon(const QString &filePath);
    // 文件搜索
    Q_INVOKABLE QStringList searchFiles(const QString &directory, const QString &pattern);
    
signals:
    void fileSelected(const QString &filePath);
    void filesSelected(const QStringList &filePaths);
    void directorySelected(const QString &dirPath);
    void selectionChanged();
    void errorOccurred(const QString &error);
    void recentFilesChanged();
    void fileOperationCompleted(const QString &operation, bool success);
    
private:
    void updateFileInfo(const QString &filePath);
    QString formatFileSize(qint64 bytes) const;
    void loadRecentFiles();
    void saveRecentFiles();
    QString detectFileType(const QFileInfo &fileInfo) const;
    
    QString m_filePath;
    QString m_fileName;
    QString m_fileSize;
    QString m_fileExtension;
    QString m_fileType;
    QString m_fileModifiedTime;
    QStringList m_selectedFiles;
    QString m_selectedDirectory;
    QStringList m_recentFiles;
    QSettings *m_settings;
    static const int MAX_RECENT_FILES = 10;
};

#endif // FILEPICKER_H

